<template>
  <transition name="fade" mode="out-in">
    <div
      v-show="value"
      class="img-preview">
      <div class="img-preview-bg" @click="close"></div>

      <div class="img-preview-rotate" @click.stop="rotate">
        <Icon size="50" type="md-refresh-circle" />
      </div>

      <div class="img-preview-close" @click.stop="close">
        <Icon size="50" type="md-close" />
      </div>

      <div class="img-preview-prev" @click.stop="prev">
        <Icon size="90" type="ios-arrow-back" />
      </div>

      <div class="img-preview-next" @click.stop="next">
        <Icon size="90" type="ios-arrow-forward" />
      </div>

      <div class="img-preview-img">
        <img
          ref="img"
          :src="imgPath"
          :style="{ transform: `rotate(${rotateDeg}deg)` }" alt="" />
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  emits: ['update:active', 'input'],
  props: {
    value: {
      type: Boolean,
      default: false
    },
    active: {
      type: Number,
      default: 0
    },
    imgArr: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    value (bol) {
      if (!bol) {
        setTimeout(() => {
          this.clearRotate()
        }, 200)
      }
    }
  },
  computed: {
    imgPath () {
      if (this.imgArr.length === 0) {
        return ''
      }

      return this.imgArr[this.active]
    }
  },
  data () {
    return {
      rotateDeg: 0
    }
  },
  methods: {
    clearRotate () {
      this.rotateDeg = 0
    },
    prev () {
      this.clearRotate()
      let index = this.active - 1
      if (index < 0) {
        index = 0
      }

      this.$emit('update:active', index)
    },
    next () {
      this.clearRotate()
      let index = this.active + 1
      if (index > this.imgArr.length - 1) {
        index = this.imgArr.length - 1
      }

      this.$emit('update:active', index)
    },
    rotate () {
      this.rotateDeg += 90
    },
    close () {
      this.$emit('input', false)
    }
  }
}
</script>

<style lang="scss" scoped>
  .img-preview {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    overflow: hidden;
    z-index: 9999;
    > .img-preview-bg {
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0 ,0.7);
    }
    > .img-preview-rotate {
      position: absolute;
      right: 100px;
      top: 30px;
      color: white;
      cursor: pointer;
      z-index: 100;
      > .iconfont {
        font-size: 30px;
      }
      &:hover {
        color: lightgray;
      }
    }
    > .img-preview-close {
      position: absolute;
      right: 40px;
      top: 30px;
      color: white;
      cursor: pointer;
      z-index: 100;
      > .iconfont {
        font-size: 30px;
      }
      &:hover {
        color: lightgray;
      }
    }
    > .img-preview-prev,
    > .img-preview-next {
      height: 40px;
      position: absolute;
      top: 0;
      bottom: 0;
      margin: auto 0;
      color: white;
      cursor: pointer;
      z-index: 100;
      > .iconfont {
        font-size: 40px;
      }
      &:hover {
        color: lightgray;
      }
    }
    > .img-preview-prev {
      left: 50px;
    }
    > .img-preview-next {
      right: 50px;
    }
    > .img-preview-img {
      max-width: 800px;
      height: 100%;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin: auto;
      display: flex;
      align-items: center;
      justify-content: center;
      @media screen and (max-width: 1500px) {
        max-width: 550px;
      }
      > img {
        // width: 100%;
        height: 100%;
      }
    }
  }
</style>
